package com.shijie.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.shijie.dto.LoginDTO;
import com.shijie.dto.RegisterDTO;
import com.shijie.dto.UserUpdateDTO;
import com.shijie.entity.User;
import com.shijie.result.SjResult;
import com.shijie.service.AccountService;
import com.shijie.vo.EnterVO;
import com.shijie.vo.TokenVO;
import com.shijie.vo.UserVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequiredArgsConstructor
public class AccountController {

    private final AccountService accountService;

    @PostMapping("/register")
    public SjResult<EnterVO> register(@RequestBody RegisterDTO registerDTO) {
        log.info("---注册服务");
        User user = accountService.register(registerDTO);
        UserVO userVO = new UserVO(user.getUserId(), user.getUsername(), user.getRole());
        log.info("register userVO: {}", userVO);

        StpUtil.login(user.getUserId());
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        TokenVO tokenVO = new TokenVO(tokenInfo.getTokenName(), tokenInfo.getTokenValue());
        log.info("register tokenVO: {}", tokenVO);

        return SjResult.success(new EnterVO(userVO, tokenVO));
    }

    @PostMapping("/login")
    public SjResult<EnterVO> login(@RequestBody LoginDTO loginDTO) {
        log.info("---登录服务loginDTO: {}", loginDTO.toString());
        User user = accountService.login(loginDTO);
        UserVO userVO = new UserVO(user.getUserId(), user.getUsername(), user.getRole());
        log.info("login userVO: {}", userVO);

        StpUtil.login(user.getUserId());
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        TokenVO tokenVO = new TokenVO(tokenInfo.getTokenName(), tokenInfo.getTokenValue());
        log.info("login tokenVO: {}", tokenVO);

        return SjResult.success(new EnterVO(userVO, tokenVO));
    }

    @GetMapping("/logout")
    public SjResult<Void> logout(@RequestHeader(name = "sa-token", required = false) String token) {
/*        if (!StpUtil.isLogin()) {
            return SjResult.error("no login");
        }*/

        if (StringUtils.hasText(token))
            StpUtil.logoutByTokenValue(token);
        else
            StpUtil.logout();

        log.info("---退出登录token【{}】", token);

        return SjResult.success();
    }

    @GetMapping("/checkCode")
    public SjResult<Integer> checkCode(@RequestParam("username") String username) {
        Integer checkCode = accountService.checkCode(username);
        log.info("---验证码: {}", checkCode);
        return SjResult.success(checkCode);
    }

    @GetMapping("/resetPassword")
    public SjResult<String> resetPassword(String username) {
        String password = accountService.resetPassword(username);
        log.info("---重置密码: {}", password);
        return SjResult.success(password);
    }

    @PostMapping("/update")
    public SjResult<Void> updateInfo(@RequestBody UserUpdateDTO userUpdateDTO) {
        boolean ok = accountService.updateInfo(userUpdateDTO);
        return SjResult.success();
    }

}
